package com.hackaton.ui;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.hackaton.MyActivity;
import com.hackaton.R;
import com.hackaton.maps.MapHelper;
import com.hackaton.maps.transport.Route;

/**
 * Created with IntelliJ IDEA.
 * User: magiman
 * Date: 10/20/13
 * Time: 11:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class GridFragment extends Fragment {

    private RelativeLayout root;
    private GridLayout grid;

    private int count;

    int type;

    public GridFragment(int type) {
        this.type = type;


    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = (RelativeLayout) inflater.inflate(R.layout.fragment_grid, null);
        grid = (GridLayout) root.findViewById(R.id.placeholder_grid);
        switch (type) {
            case 0:
                initTaxi();
                break;
            case 1:
                initTrol();
                break;
            case 2:
                initTram();
                break;
        }
        return root;
    }

    public void initTaxi() {
        addTaxiButton("1Б");
    }

    public void initTram() {
        addTramButton("1");
        addTramButton("2");
        addTramButton("4");
        addTramButton("5");
        addTramButton("6");
    }

    public void initTrol() {
        addTrolButton("1");
        addTrolButton("2");
        addTrolButton("3");
        addTrolButton("4");
        addTrolButton("5");
        addTrolButton("6");
        addTrolButton("7");
        addTrolButton("8");
        addTrolButton("9");
        addTrolButton("10");
        addTrolButton("11");
        addTrolButton("12");
        addTrolButton("13");
        addTrolButton("15");
    }

    private void addTramButton(final String labelText) {
        LinearLayout row = new LinearLayout(getActivity());
        TextView text = new TextView(getActivity());
        ToggleButton button = new ToggleButton(getActivity());
        count++;
        button.setTextOff(labelText);
        button.setTextOn(labelText);
        button.setBackgroundResource(R.drawable.bg_toggle_route);
        button.setText(String.valueOf(labelText));
        Route route = MapHelper.getInstance().routesTram.get(labelText);
        if (route == null)                   {
            button.setVisibility(View.GONE);
            return;
        }
        button.setChecked(route.getVisible());
        button.setWidth(70);
        button.setHeight(70);
        button.setPadding(15, 15, 15, 15);

        text.setText(route.stations.get(0).address + "\n" +
                route.stations.get(route.stations.size() - 1).address);
        row.addView(button);
        row.addView(text);
        grid.addView(row);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MapHelper.getInstance().routesTram.get(labelText).toggleVisible();
                //TODO change route visible
            }
        });
    }

    private void addTaxiButton(final String labelText) {
        TextView text = new TextView(getActivity());

        ToggleButton button = new ToggleButton(MyActivity.getContext());
        count++;
        button.setBackgroundResource(R.drawable.bg_toggle_route);
        button.setText(String.valueOf(count));
        button.setWidth(70);
        button.setHeight(70);

        button.setPadding(15, 15, 15, 15);
        button.setTextOff(labelText);
        button.setTextOn(labelText);

        Route route = MapHelper.getInstance().routesTaxi.get(labelText);
        if (route == null) {
            button.setVisibility(View.GONE);
            return;
        }

        text.setText(route.stations.get(0).address + "\n" +
                route.stations.get(route.stations.size() - 1).address);

        grid.addView(button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MapHelper.getInstance().routesTaxi.get(labelText).toggleVisible();
                //TODO change route visible
            }
        });
    }

    private void addTrolButton(final String labelText) {
        LinearLayout row = new LinearLayout(getActivity());

        TextView text = new TextView(getActivity());


        ToggleButton button = new ToggleButton(getActivity());
        row.addView(button);
        row.addView(text);
        count++;
        button.setBackgroundResource(R.drawable.bg_toggle_route);
        button.setText(String.valueOf(count));
        button.setWidth(70);
        button.setHeight(70);
        button.setPadding(15, 15, 15, 15);
        button.setTextOff(labelText);
        button.setTextOn(labelText);

        final Route route = MapHelper.getInstance().routesTrol.get(labelText);
        if (route == null)                   {
            button.setVisibility(View.GONE);
            return;
        }

        text.setText(route.stations.get(0).address + "\n" +
                route.stations.get(route.stations.size() - 1).address);

        grid.addView(row);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                route.toggleVisible();
                //TODO change route visible
            }
        });
    }

    public void clean() {
        if (grid != null) {
            count = 0;
            grid.removeAllViews();
        }
    }
}
